export default {
	namespaced:true,
	state:()=>({
		popShow:false,
		popData:null,
		cartList:[] //购物车列表
	}),
	getters:{
		statistics(state){
			let allCheck = true  //假设全选
			let totalPrice = 0   
			state.cartList.forEach((item,i)=>{
				if(!item.ischeck){
					allCheck = false
				}
				if(item.ischeck){
					let {price} = item.list[item.dropIdx]
					totalPrice+=price*item.num
				}
			})
			return {
				allCheck,
				totalPrice
			}
		}
	},
	mutations:{
		popShowMut(state,bool){
			state.popShow = bool
		},
		popDataMut(state,goodsObj){
			state.popData = goodsObj
		},
		cartAddMut(state,cartObj){
			let {objectId,num,dropIdx} = cartObj
			let {cartList} = state
			for(let i=0;i<cartList.length;i++){
				if(objectId==cartList[i].objectId&&dropIdx==cartList[i].dropIdx){
					cartList[i].num += num //修改相同商品的数量
					return
				}
			}
			//新增新商品
			state.cartList.push(cartObj)
		},
		//单选
		cartCheckMut(state,idx){
			state.cartList[idx].ischeck = !state.cartList[idx].ischeck
		},
		//全选
		cartAllCheckMut(state,bool){
			state.cartList.forEach(item=>{
				item.ischeck = !bool
			})
		}
	}
}